﻿using System.Collections.Generic;
using System.Xml.Linq;
using QASPRibbon.Commands;
using Schema = QASPRibbon.RibbonSchema;

namespace QASPRibbon.Controls
{
    /// <summary>
    /// Gallery button
    /// </summary>
    public class RibbonGalleryButton : RibbonButton
    {
        internal override string XmlTag
        {
            get { return Schema.GalleryButton; }
        }

        internal override XElement AsXElement()
        {
            var element = base.AsXElement();
            element.Attribute(Schema.LabelText).Remove();
            element.Add(new XAttribute(Schema.ElementDimensions, ElementDimensions.ToString()));
            if (!string.IsNullOrEmpty(ImageUrl))
                element.Add(new XAttribute(Schema.Image, ImageUrl));
            if (!string.IsNullOrEmpty(ImageClass))
                element.Add(new XAttribute(Schema.ImageClass, ImageClass));
            element.Add(new XAttribute(Schema.ImageTop, ImageTop));
            element.Add(new XAttribute(Schema.ImageLeft, ImageLeft));
            element.Add(new XAttribute(Schema.CommandType, CommandType.ToString()));
            if (!string.IsNullOrEmpty(InnerHTML))
                element.Add(new XAttribute(Schema.InnerHTML, InnerHTML));
            if (!string.IsNullOrEmpty(CommandArgument))
                element.Add(new XAttribute(Schema.CommandValueId, CommandArgument));
            if (!string.IsNullOrEmpty(CommandArgument))
                element.Add(new XAttribute(Schema.CommandPreview, OnClientPreview));
            if (!string.IsNullOrEmpty(CommandArgument))
                element.Add(new XAttribute(Schema.CommandRevert, OnClientRevert));
            
            
            return element;
        }

        internal override IEnumerable<RibbonCommand> ClientCommands
        {
            get
            {
                var res = new List<RibbonCommand>();
                var baseList = base.ClientCommands;
                res.AddRange(baseList);
                res.Add(new RibbonCommand(FullId, RibbonSchema.CommandPreview,
                                          string.Format("{0}; {1}; return {2};", OnClientPreview ?? string.Empty,
                                                        OnClientPreview, Enabled ? "true" : "false")));
                res.Add(new RibbonCommand(FullId, RibbonSchema.CommandRevert,
                                          string.Format("{0}; {1}; return {2};", OnClientRevert ?? string.Empty,
                                                        OnClientRevert, Enabled ? "true" : "false")));

                return res;
            }
        }

        /// <summary>
        /// A server-relative URL
        /// </summary>
        public string ImageUrl
        {
            get { return _imageUrl; }
            set { _imageUrl = value; }
        }

        public string ImageClass
        {
            get { return _imageClass; }
            set { _imageClass = value; }
        }

        public int ImageLeft
        {
            get { return _imageLeft; }
            set { _imageLeft = value; }
        }

        public int ImageTop
        {
            get { return _imageTop; }
            set { _imageTop = value; }
        }

        public RibbonGalleryElementSize ElementDimensions
        {
            get { return _elementDimension; }
            set { _elementDimension = value; }
        }

        public string InnerHTML
        {
            get { return _innerHTML; }
            set { _innerHTML = value; }
        }

        public string CommandArgument
        {
            get { return _commandArgument; }
            set { _commandArgument = value; }
        }

        public string OnClientPreview
        {
            get { return _onClientPreview; }
            set { _onClientPreview = value; }
        }

        public string OnClientRevert
        {
            get { return _onClientRevert; }
            set { _onClientRevert = value; }
        }

        public RibbonCommandType CommandType
        {
            get { return _commandType; }
            set { _commandType = value; }
        }

        public RibbonGalleryButton() { }

        public RibbonGalleryButton(string id) : base(id) { }

        public RibbonGalleryButton(string id, string title) : base(id, title) { }

        private RibbonGalleryElementSize _elementDimension = RibbonGalleryElementSize.Size48by48;
        private string _innerHTML;
        private string _commandArgument;
        private string _onClientPreview;
        private string _onClientRevert;
        private RibbonCommandType _commandType = RibbonCommandType.General;
        private int _imageTop;
        private int _imageLeft;
        private string _imageClass;
        private string _imageUrl;
    }
}
